package jackson;

import org.codehaus.jackson.JsonParseException;
import org.codehaus.jackson.map.JsonMappingException;
import org.codehaus.jackson.map.ObjectMapper;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by haiqiang on 17/5/21.
 */
public class JacksonSimpleDataBinding {
    public static void main(String args[]) {
        JacksonSimpleDataBinding simpleDataBinding = new JacksonSimpleDataBinding();

        try {
            ObjectMapper mapper = new ObjectMapper();

            Map<String, Object> studentDataMap = new HashMap<String, Object>();
            int[] marks = {1,2,3};

            Student student = new Student();

            student.setAge(10);
            student.setName("Mahesh");

            // JAVA Object
            studentDataMap.put("student", student);
            // JAVA String
            studentDataMap.put("name", "Mahesh kumar");
            // JAVA Boolean
            studentDataMap.put("verified", Boolean.FALSE);

            // Array
            studentDataMap.put("marks", marks);


            System.out.println(mapper.writeValueAsString(studentDataMap));
        } catch (JsonParseException e) {
            e.printStackTrace();
        } catch (JsonMappingException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

class Student {
    private String name;
    private int age;

    public Student() {

    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public String toString() {
        return "Student [ name: " + name + ", age: " + age + " ]";
    }
}
